//
//  AdminInfo.swift
//  PerfectServer
//
//  Created by 王薰怡 on 2017/6/28.
//
//

import Foundation
import StORM
import MySQLStORM

class AdminInfo: MySQLStORM {
    var id: Int32 = 0
    var name: String?
    var email: String = ""
    var mobile: String?
    var password: String = ""
    var salt: String = ""
    
    override func table() -> String {
        return "admin_info"
    }
    
    override func to(_ this: StORMRow) {
        id = this.data["id"] as! Int32
        name = this.data["name"] as? String
        email = this.data["email"] as! String
        mobile = this.data["mobile"] as? String
        password = this.data["password"] as! String
        salt = this.data["salt"] as! String
    }
    
    func row() -> [AdminInfo] {
        var rows: [AdminInfo] = []
        for row in self.results.rows {
            let admin = AdminInfo()
            admin.to(row)
            rows.append(admin)
        }
        return rows
    }
}

struct AdminInfoDAO {
    
    static func findBy(email: String) -> AdminInfo? {
        let admin = AdminInfo()
        do {
            try admin.find([("email", email)])
            let result = admin.row()
            if result.count == 1 {
                return result.first
            } else {
                return nil
            }
        } catch {
            fatalError("Error: An error occurred while finding records in \(type(of: self))")
        }
    }

    

}

